<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>trie_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_structs_trie_test.js.html">trie_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.structs.TrieTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.structs.TrieTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.object&#39;);
<a name="line19"></a>goog.require(&#39;goog.structs&#39;);
<a name="line20"></a>goog.require(&#39;goog.structs.Trie&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>
<a name="line23"></a>function makeTrie() {
<a name="line24"></a>  var trie = new goog.structs.Trie();
<a name="line25"></a>  trie.add(&#39;hello&#39;, 1);
<a name="line26"></a>  trie.add(&#39;hi&#39;, &#39;howdy&#39;);
<a name="line27"></a>  trie.add(&#39;&#39;, &#39;an empty string key&#39;);
<a name="line28"></a>  trie.add(&#39;empty value&#39;, &#39;&#39;);
<a name="line29"></a>  trie.add(&#39;zero&#39;, 0);
<a name="line30"></a>  trie.add(&#39;object&#39;, {});
<a name="line31"></a>  trie.add(&#39;null&#39;, null);
<a name="line32"></a>  trie.add(&#39;hello, world&#39;, 2);
<a name="line33"></a>  trie.add(&#39;world&#39;, {});
<a name="line34"></a>  return trie;
<a name="line35"></a>}
<a name="line36"></a>
<a name="line37"></a>function checkTrie(trie) {
<a name="line38"></a>  assertEquals(&#39;get, should be 1&#39;, trie.get(&#39;hello&#39;), 1);
<a name="line39"></a>  assertEquals(&#39;get, should be &quot;howdy&quot;&#39;, trie.get(&#39;hi&#39;), &#39;howdy&#39;);
<a name="line40"></a>  assertEquals(&#39;get, should be &quot;an empty string key&quot;&#39;, trie.get(&#39;&#39;),
<a name="line41"></a>      &#39;an empty string key&#39;);
<a name="line42"></a>  assertEquals(&#39;get, should be &quot;&quot;&#39;, trie.get(&#39;empty value&#39;), &#39;&#39;);
<a name="line43"></a>  assertEquals(&#39;get, should be &quot;&quot;&#39;, typeof trie.get(&#39;empty value&#39;), &#39;string&#39;);
<a name="line44"></a>  assertEquals(&#39;get, should be an object&#39;, typeof trie.get(&#39;object&#39;), &#39;object&#39;);
<a name="line45"></a>  assertEquals(&#39;get, should be 0&#39;, trie.get(&#39;zero&#39;), 0);
<a name="line46"></a>  assertEquals(&#39;get &quot;null&quot;, should be null&#39;, trie.get(&#39;null&#39;), null);
<a name="line47"></a>  assertEquals(&#39;get, should be 2&#39;, trie.get(&#39;hello, world&#39;), 2);
<a name="line48"></a>  assertEquals(&#39;get, should be an object&#39;, typeof trie.get(&#39;world&#39;), &#39;object&#39;);
<a name="line49"></a>}
<a name="line50"></a>
<a name="line51"></a>function testTrieFormation() {
<a name="line52"></a>  var t = makeTrie();
<a name="line53"></a>  checkTrie(t);
<a name="line54"></a>}
<a name="line55"></a>
<a name="line56"></a>function testFailureOfMultipleAdds() {
<a name="line57"></a>  var t = new goog.structs.Trie();
<a name="line58"></a>  t.add(&#39;hello&#39;, &#39;testing&#39;);
<a name="line59"></a>  assertThrows(&#39;Error should be thrown when same key added twice.&#39;, function() {
<a name="line60"></a>    t.add(&#39;hello&#39;, &#39;test&#39;);
<a name="line61"></a>  });
<a name="line62"></a>
<a name="line63"></a>  t = new goog.structs.Trie();
<a name="line64"></a>  t.add(&#39;null&#39;, null);
<a name="line65"></a>  assertThrows(&#39;Error should be thrown when same key added twice.&#39;, function() {
<a name="line66"></a>    t.add(&#39;null&#39;, &#39;hi!&#39;);
<a name="line67"></a>  });
<a name="line68"></a>
<a name="line69"></a>  t = new goog.structs.Trie();
<a name="line70"></a>  t.add(&#39;null&#39;, &#39;blah&#39;);
<a name="line71"></a>  assertThrows(&#39;Error should be thrown when same key added twice.&#39;, function() {
<a name="line72"></a>    t.add(&#39;null&#39;, null);
<a name="line73"></a>  });
<a name="line74"></a>}
<a name="line75"></a>
<a name="line76"></a>function testTrieClone() {
<a name="line77"></a>  var trieOne = makeTrie();
<a name="line78"></a>  var trieTwo = new goog.structs.Trie(trieOne);
<a name="line79"></a>  checkTrie(trieTwo);
<a name="line80"></a>}
<a name="line81"></a>
<a name="line82"></a>function testTrieFromObject() {
<a name="line83"></a>  var someObject = {&#39;hello&#39; : 1,
<a name="line84"></a>    &#39;hi&#39; : &#39;howdy&#39;,
<a name="line85"></a>    &#39;&#39; : &#39;an empty string key&#39;,
<a name="line86"></a>    &#39;empty value&#39; : &#39;&#39;,
<a name="line87"></a>    &#39;object&#39; : {},
<a name="line88"></a>    &#39;zero&#39; : 0,
<a name="line89"></a>    &#39;null&#39; : null,
<a name="line90"></a>    &#39;hello, world&#39; : 2,
<a name="line91"></a>    &#39;world&#39; : {}};
<a name="line92"></a>  var trie = new goog.structs.Trie(someObject);
<a name="line93"></a>  checkTrie(trie);
<a name="line94"></a>}
<a name="line95"></a>
<a name="line96"></a>function testTrieGetValues() {
<a name="line97"></a>  var trie = makeTrie();
<a name="line98"></a>  var values = trie.getValues();
<a name="line99"></a>  assertTrue(&#39;getValues, should contain &quot;howdy&quot;&#39;,
<a name="line100"></a>      goog.object.contains(values, &#39;howdy&#39;));
<a name="line101"></a>  assertTrue(&#39;getValues, should contain 1&#39;, goog.object.contains(values, 1));
<a name="line102"></a>  assertTrue(&#39;getValues, should contain 0&#39;, goog.object.contains(values, 0));
<a name="line103"></a>  assertTrue(&#39;getValues, should contain &quot;&quot;&#39;, goog.object.contains(values, &#39;&#39;));
<a name="line104"></a>  assertTrue(&#39;getValues, should contain null&#39;,
<a name="line105"></a>      goog.object.contains(values, null));
<a name="line106"></a>  assertEquals(&#39;goog.structs.getCount(getValues()) should be 9&#39;,
<a name="line107"></a>      goog.structs.getCount(values), 9);
<a name="line108"></a>}
<a name="line109"></a>
<a name="line110"></a>function testTrieGetKeys() {
<a name="line111"></a>  var trie = makeTrie();
<a name="line112"></a>  var keys = trie.getKeys();
<a name="line113"></a>  assertTrue(&#39;getKeys, should contain &quot;hello&quot;&#39;,
<a name="line114"></a>      goog.object.contains(keys, &#39;hello&#39;));
<a name="line115"></a>  assertTrue(&#39;getKeys, should contain &quot;empty value&quot;&#39;,
<a name="line116"></a>      goog.object.contains(keys, &#39;empty value&#39;));
<a name="line117"></a>  assertTrue(&#39;getKeys, should contain &quot;&quot;&#39;, goog.object.contains(keys, &#39;&#39;));
<a name="line118"></a>  assertTrue(&#39;getKeys, should contain &quot;zero&quot;&#39;,
<a name="line119"></a>      goog.object.contains(keys, &#39;zero&#39;));
<a name="line120"></a>  assertEquals(&#39;goog.structs.getCount(getKeys()) should be 9&#39;,
<a name="line121"></a>      goog.structs.getCount(keys), 9);
<a name="line122"></a>}
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>function testTrieCount() {
<a name="line126"></a>  var trieOne = makeTrie();
<a name="line127"></a>  var trieTwo = new goog.structs.Trie();
<a name="line128"></a>  assertEquals(&#39;count, should be 9&#39;, trieOne.getCount(), 9);
<a name="line129"></a>  assertEquals(&#39;count, should be 0&#39;, trieTwo.getCount(), 0);
<a name="line130"></a>}
<a name="line131"></a>
<a name="line132"></a>function testRemoveKeyFromTrie() {
<a name="line133"></a>  var trie = new goog.structs.Trie();
<a name="line134"></a>  trie.add(&#39;key1&#39;, &#39;value1&#39;);
<a name="line135"></a>  trie.add(&#39;key2&#39;, &#39;value2&#39;);
<a name="line136"></a>  trie.add(&#39;ke&#39;, &#39;value3&#39;);
<a name="line137"></a>  trie.add(&#39;zero&#39;, 0);
<a name="line138"></a>  trie.remove(&#39;key2&#39;);
<a name="line139"></a>  assertEquals(&#39;get &quot;key1&quot;, should be &quot;value1&quot;&#39;, trie.get(&#39;key1&#39;), &#39;value1&#39;);
<a name="line140"></a>  assertUndefined(&#39;get &quot;key2&quot;, should be undefined&#39;, trie.get(&#39;key2&#39;));
<a name="line141"></a>  trie.remove(&#39;zero&#39;);
<a name="line142"></a>  assertUndefined(&#39;get &quot;zero&quot;, should be undefined&#39;, trie.get(&#39;zero&#39;));
<a name="line143"></a>  trie.remove(&#39;ke&#39;);
<a name="line144"></a>  assertUndefined(&#39;get &quot;ke&quot;, should be undefined&#39;, trie.get(&#39;ke&#39;));
<a name="line145"></a>  assertEquals(&#39;get &quot;key1&quot;, should be &quot;value1&quot;&#39;, trie.get(&#39;key1&#39;), &#39;value1&#39;);
<a name="line146"></a>  trie.add(&#39;a&#39;, &#39;value4&#39;);
<a name="line147"></a>  assertTrue(&#39;testing internal structure, a should be a child&#39;,
<a name="line148"></a>      &#39;a&#39; in trie.childNodes_);
<a name="line149"></a>  trie.remove(&#39;a&#39;);
<a name="line150"></a>  assertFalse(&#39;testing internal structure, a should no longer be a child&#39;,
<a name="line151"></a>      &#39;a&#39; in trie.childNodes_);
<a name="line152"></a>
<a name="line153"></a>  trie.add(&#39;xyza&#39;, &#39;value&#39;);
<a name="line154"></a>  trie.remove(&#39;xyza&#39;, &#39;value&#39;);
<a name="line155"></a>  assertFalse(&#39;Should not have &quot;x&quot;&#39;, &#39;x&#39; in trie.childNodes_);
<a name="line156"></a>
<a name="line157"></a>  trie.add(&#39;xyza&#39;, null);
<a name="line158"></a>  assertTrue(&#39;Should have &quot;x&quot;&#39;, &#39;x&#39; in trie.childNodes_);
<a name="line159"></a>  trie.remove(&#39;xyza&#39;);
<a name="line160"></a>  assertFalse(&#39;Should not have &quot;x&quot;&#39;, &#39;x&#39; in trie.childNodes_);
<a name="line161"></a>
<a name="line162"></a>  trie.add(&#39;xyza&#39;, &#39;value&#39;);
<a name="line163"></a>  trie.add(&#39;xb&#39;, &#39;value&#39;);
<a name="line164"></a>  trie.remove(&#39;xyza&#39;);
<a name="line165"></a>  assertTrue(&#39;get &quot;x&quot; should be defined&#39;, &#39;x&#39; in trie.childNodes_);
<a name="line166"></a>  assertFalse(&#39;get &quot;y&quot; should be undefined&#39;,
<a name="line167"></a>      &#39;y&#39; in trie.childNodes_[&#39;x&#39;].childNodes_);
<a name="line168"></a>
<a name="line169"></a>  trie.add(&#39;akey&#39;, &#39;value1&#39;);
<a name="line170"></a>  trie.add(&#39;akey1&#39;, &#39;value2&#39;);
<a name="line171"></a>  trie.remove(&#39;akey1&#39;);
<a name="line172"></a>  assertEquals(&#39;get &quot;akey&quot;, should be &quot;value1&quot;&#39;, &#39;value1&#39;, trie.get(&#39;akey&#39;));
<a name="line173"></a>  assertUndefined(&#39;get &quot;akey1&quot;, should be undefined&#39;, trie.get(&#39;akey1&#39;));
<a name="line174"></a>}
<a name="line175"></a>
<a name="line176"></a>function testRemoveKeyFromTrieWithNulls() {
<a name="line177"></a>  var trie = new goog.structs.Trie();
<a name="line178"></a>  trie.add(&#39;key1&#39;, null);
<a name="line179"></a>  trie.add(&#39;key2&#39;, &#39;value2&#39;);
<a name="line180"></a>  trie.add(&#39;ke&#39;, &#39;value3&#39;);
<a name="line181"></a>  trie.add(&#39;zero&#39;, 0);
<a name="line182"></a>  trie.remove(&#39;key2&#39;);
<a name="line183"></a>  assertEquals(&#39;get &quot;key1&quot;, should be null&#39;, trie.get(&#39;key1&#39;), null);
<a name="line184"></a>  assertUndefined(&#39;get &quot;key2&quot;, should be undefined&#39;, trie.get(&#39;key2&#39;));
<a name="line185"></a>  trie.remove(&#39;zero&#39;);
<a name="line186"></a>  assertUndefined(&#39;get &quot;zero&quot;, should be undefined&#39;, trie.get(&#39;zero&#39;));
<a name="line187"></a>  trie.remove(&#39;ke&#39;);
<a name="line188"></a>  assertUndefined(&#39;get &quot;ke&quot;, should be undefined&#39;, trie.get(&#39;ke&#39;));
<a name="line189"></a>  assertEquals(&#39;get &quot;key1&quot;, should be null&#39;, trie.get(&#39;key1&#39;), null);
<a name="line190"></a>  trie.add(&#39;a&#39;, &#39;value4&#39;);
<a name="line191"></a>  assertTrue(&#39;testing internal structure, a should be a child&#39;,
<a name="line192"></a>      &#39;a&#39; in trie.childNodes_);
<a name="line193"></a>  trie.remove(&#39;a&#39;);
<a name="line194"></a>  assertFalse(&#39;testing internal structure, a should no longer be a child&#39;,
<a name="line195"></a>      &#39;a&#39; in trie.childNodes_);
<a name="line196"></a>
<a name="line197"></a>  trie.add(&#39;xyza&#39;, null);
<a name="line198"></a>  trie.add(&#39;xb&#39;, &#39;value&#39;);
<a name="line199"></a>  trie.remove(&#39;xyza&#39;);
<a name="line200"></a>  assertTrue(&#39;Should have &quot;x&quot;&#39;, &#39;x&#39; in trie.childNodes_);
<a name="line201"></a>  assertFalse(&#39;Should not have &quot;y&quot;&#39;,
<a name="line202"></a>      &#39;y&#39; in trie.childNodes_[&#39;x&#39;].childNodes_);
<a name="line203"></a>}
<a name="line204"></a>
<a name="line205"></a>function testRemoveKeyException() {
<a name="line206"></a>  var trie = new goog.structs.Trie();
<a name="line207"></a>  trie.add(&#39;abcdefg&#39;, &#39;value&#39;);
<a name="line208"></a>  trie.add(&#39;abcz&#39;, &#39;value&#39;);
<a name="line209"></a>  trie.add(&#39;abc&#39;, &#39;value&#39;);
<a name="line210"></a>
<a name="line211"></a>  assertThrows(&#39;Remove should throw an error on removal of non-existent key&#39;,
<a name="line212"></a>      function() {
<a name="line213"></a>        trie.remove(&#39;abcdefge&#39;);
<a name="line214"></a>      });
<a name="line215"></a>}
<a name="line216"></a>
<a name="line217"></a>function testTrieIsEmpty() {
<a name="line218"></a>  var trieOne = new goog.structs.Trie();
<a name="line219"></a>  var trieTwo = makeTrie();
<a name="line220"></a>  assertTrue(&#39;isEmpty, should be empty&#39;, trieOne.isEmpty());
<a name="line221"></a>  assertFalse(&#39;isEmpty, should not be empty&#39;, trieTwo.isEmpty());
<a name="line222"></a>  trieOne.add(&#39;&#39;, 1);
<a name="line223"></a>  assertFalse(&#39;isEmpty, should not be empty&#39;, trieTwo.isEmpty());
<a name="line224"></a>  trieOne.remove(&#39;&#39;);
<a name="line225"></a>  assertTrue(&#39;isEmpty, should be empty&#39;, trieOne.isEmpty());
<a name="line226"></a>  trieOne.add(&#39;&#39;, 1);
<a name="line227"></a>  trieOne.add(&#39;a&#39;, 1);
<a name="line228"></a>  trieOne.remove(&#39;a&#39;);
<a name="line229"></a>  assertFalse(&#39;isEmpty, should not be empty&#39;, trieOne.isEmpty());
<a name="line230"></a>  trieOne.remove(&#39;&#39;);
<a name="line231"></a>  assertTrue(&#39;isEmpty, should be empty&#39;, trieOne.isEmpty());
<a name="line232"></a>  trieOne.add(&#39;&#39;, 1);
<a name="line233"></a>  trieOne.add(&#39;a&#39;, 1);
<a name="line234"></a>  trieOne.remove(&#39;&#39;);
<a name="line235"></a>  assertFalse(&#39;isEmpty, should not be empty&#39;, trieOne.isEmpty());
<a name="line236"></a>  trieOne.remove(&#39;a&#39;);
<a name="line237"></a>  assertTrue(&#39;isEmpty, should be empty&#39;, trieOne.isEmpty());
<a name="line238"></a>}
<a name="line239"></a>
<a name="line240"></a>function testTrieClear() {
<a name="line241"></a>  var trie = new goog.structs.Trie();
<a name="line242"></a>  trie.add(&#39;key1&#39;, &#39;value1&#39;);
<a name="line243"></a>  trie.add(&#39;key2&#39;, &#39;value2&#39;);
<a name="line244"></a>  trie.add(&#39;key3&#39;, null);
<a name="line245"></a>  trie.clear();
<a name="line246"></a>  assertUndefined(&#39;get key1, should be undefined&#39;, trie.get(&#39;key1&#39;));
<a name="line247"></a>  assertUndefined(&#39;get key2, should be undefined&#39;, trie.get(&#39;key2&#39;));
<a name="line248"></a>  assertUndefined(&#39;get key3, should be undefined&#39;, trie.get(&#39;key3&#39;));
<a name="line249"></a>}
<a name="line250"></a>
<a name="line251"></a>function testTrieContainsKey() {
<a name="line252"></a>  var trie = makeTrie();
<a name="line253"></a>  assertTrue(&#39;containsKey, should contain &quot;hello&quot;&#39;, trie.containsKey(&#39;hello&#39;));
<a name="line254"></a>  assertTrue(&#39;containsKey, should contain &quot;hi&quot;&#39;, trie.containsKey(&#39;hi&#39;));
<a name="line255"></a>  assertTrue(&#39;containsKey, should contain &quot;&quot;&#39;, trie.containsKey(&#39;&#39;));
<a name="line256"></a>  assertTrue(&#39;containsKey, should contain &quot;empty value&quot;&#39;,
<a name="line257"></a>      trie.containsKey(&#39;empty value&#39;));
<a name="line258"></a>  assertTrue(&#39;containsKey, should contain &quot;object&quot;&#39;,
<a name="line259"></a>      trie.containsKey(&#39;object&#39;));
<a name="line260"></a>  assertTrue(&#39;containsKey, should contain &quot;zero&quot;&#39;, trie.containsKey(&#39;zero&#39;));
<a name="line261"></a>  assertTrue(&#39;containsKey, should contain &quot;null&quot;&#39;, trie.containsKey(&#39;null&#39;));
<a name="line262"></a>  assertFalse(&#39;containsKey, should not contain &quot;blah&quot;&#39;,
<a name="line263"></a>      trie.containsKey(&#39;blah&#39;));
<a name="line264"></a>  trie.remove(&#39;&#39;);
<a name="line265"></a>  trie.remove(&#39;hi&#39;);
<a name="line266"></a>  trie.remove(&#39;zero&#39;);
<a name="line267"></a>  trie.remove(&#39;null&#39;);
<a name="line268"></a>  assertFalse(&#39;containsKey, should not contain &quot;zero&quot;&#39;,
<a name="line269"></a>      trie.containsKey(&#39;zero&#39;));
<a name="line270"></a>  assertFalse(&#39;containsKey, should not contain &quot;&quot;&#39;, trie.containsKey(&#39;&#39;));
<a name="line271"></a>  assertFalse(&#39;containsKey, should not contain &quot;hi&quot;&#39;, trie.containsKey(&#39;hi&#39;));
<a name="line272"></a>  assertFalse(&#39;containsKey, should not contain &quot;null&quot;&#39;,
<a name="line273"></a>      trie.containsKey(&#39;null&#39;));
<a name="line274"></a>}
<a name="line275"></a>
<a name="line276"></a>function testTrieContainsPrefix() {
<a name="line277"></a>
<a name="line278"></a>  // Empty trie.
<a name="line279"></a>  var trie = new goog.structs.Trie();
<a name="line280"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;&quot;&#39;, trie.containsPrefix(&#39;&#39;));
<a name="line281"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;any&quot;&#39;,
<a name="line282"></a>      trie.containsPrefix(&#39;any&#39;));
<a name="line283"></a>  trie.add(&#39;key&#39;, &#39;value&#39;);
<a name="line284"></a>  assertTrue(&#39;containsPrefix, should contain &quot;&quot;&#39;, trie.containsPrefix(&#39;&#39;));
<a name="line285"></a>
<a name="line286"></a>  // Non-empty trie.
<a name="line287"></a>  trie = makeTrie();
<a name="line288"></a>  assertTrue(&#39;containsPrefix, should contain &quot;&quot;&#39;, trie.containsPrefix(&#39;&#39;));
<a name="line289"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;blah&quot;&#39;,
<a name="line290"></a>      trie.containsPrefix(&#39;blah&#39;));
<a name="line291"></a>  assertTrue(&#39;containsPrefix, should contain &quot;h&quot;&#39;, trie.containsPrefix(&#39;h&#39;));
<a name="line292"></a>  assertTrue(&#39;containsPrefix, should contain &quot;hello&quot;&#39;,
<a name="line293"></a>      trie.containsPrefix(&#39;hello&#39;));
<a name="line294"></a>  assertTrue(&#39;containsPrefix, should contain &quot;hello, world&quot;&#39;,
<a name="line295"></a>      trie.containsPrefix(&#39;hello, world&#39;));
<a name="line296"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;hello, world!&quot;&#39;,
<a name="line297"></a>      trie.containsPrefix(&#39;hello, world!&#39;));
<a name="line298"></a>  assertTrue(&#39;containsPrefix, should contain &quot;nu&quot;&#39;,
<a name="line299"></a>      trie.containsPrefix(&#39;nu&#39;));
<a name="line300"></a>  assertTrue(&#39;containsPrefix, should contain &quot;null&quot;&#39;,
<a name="line301"></a>      trie.containsPrefix(&#39;null&#39;));
<a name="line302"></a>  assertTrue(&#39;containsPrefix, should contain &quot;empty value&quot;&#39;,
<a name="line303"></a>      trie.containsPrefix(&#39;empty value&#39;));
<a name="line304"></a>
<a name="line305"></a>  // Remove nodes.
<a name="line306"></a>  trie.remove(&#39;&#39;);
<a name="line307"></a>  assertTrue(&#39;containsPrefix, should contain &quot;&quot;&#39;, trie.containsPrefix(&#39;&#39;));
<a name="line308"></a>  trie.remove(&#39;hi&#39;);
<a name="line309"></a>  assertTrue(&#39;containsPrefix, should contain &quot;h&quot;&#39;, trie.containsPrefix(&#39;h&#39;));
<a name="line310"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;hi&quot;&#39;,
<a name="line311"></a>      trie.containsPrefix(&#39;hi&#39;));
<a name="line312"></a>  trie.remove(&#39;hello&#39;);
<a name="line313"></a>  trie.remove(&#39;hello, world&#39;);
<a name="line314"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;h&quot;&#39;,
<a name="line315"></a>      trie.containsPrefix(&#39;h&#39;));
<a name="line316"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;hello&quot;&#39;,
<a name="line317"></a>      trie.containsPrefix(&#39;hello&#39;));
<a name="line318"></a>
<a name="line319"></a>  // Remove all nodes.
<a name="line320"></a>  trie.remove(&#39;empty value&#39;);
<a name="line321"></a>  trie.remove(&#39;zero&#39;);
<a name="line322"></a>  trie.remove(&#39;object&#39;);
<a name="line323"></a>  trie.remove(&#39;null&#39;);
<a name="line324"></a>  trie.remove(&#39;world&#39;);
<a name="line325"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;&quot;&#39;,
<a name="line326"></a>      trie.containsPrefix(&#39;&#39;));
<a name="line327"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;h&quot;&#39;,
<a name="line328"></a>      trie.containsPrefix(&#39;h&#39;));
<a name="line329"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;hi&quot;&#39;,
<a name="line330"></a>      trie.containsPrefix(&#39;hi&#39;));
<a name="line331"></a>
<a name="line332"></a>  // Add some new nodes.
<a name="line333"></a>  trie.add(&#39;hi&#39;, &#39;value&#39;);
<a name="line334"></a>  trie.add(&#39;null&#39;, &#39;value&#39;);
<a name="line335"></a>  assertTrue(&#39;containsPrefix, should contain &quot;&quot;&#39;, trie.containsPrefix(&#39;&#39;));
<a name="line336"></a>  assertTrue(&#39;containsPrefix, should contain &quot;h&quot;&#39;, trie.containsPrefix(&#39;h&#39;));
<a name="line337"></a>  assertTrue(&#39;containsPrefix, should contain &quot;hi&quot;&#39;, trie.containsPrefix(&#39;hi&#39;));
<a name="line338"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;hello&quot;&#39;,
<a name="line339"></a>      trie.containsPrefix(&#39;hello&#39;));
<a name="line340"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;zero&quot;&#39;,
<a name="line341"></a>      trie.containsPrefix(&#39;zero&#39;));
<a name="line342"></a>
<a name="line343"></a>  // Clear the trie.
<a name="line344"></a>  trie.clear();
<a name="line345"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;&quot;&#39;,
<a name="line346"></a>      trie.containsPrefix(&#39;&#39;));
<a name="line347"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;h&quot;&#39;,
<a name="line348"></a>      trie.containsPrefix(&#39;h&#39;));
<a name="line349"></a>  assertFalse(&#39;containsPrefix, should not contain &quot;hi&quot;&#39;,
<a name="line350"></a>      trie.containsPrefix(&#39;hi&#39;));
<a name="line351"></a>}
<a name="line352"></a>
<a name="line353"></a>function testTrieContainsValue() {
<a name="line354"></a>  var trie = makeTrie();
<a name="line355"></a>  assertTrue(&#39;containsValue, should be true, should contain 1&#39;,
<a name="line356"></a>      trie.containsValue(1));
<a name="line357"></a>  assertTrue(&#39;containsValue, should be true, should contain &quot;howdy&quot;&#39;,
<a name="line358"></a>      trie.containsValue(&#39;howdy&#39;));
<a name="line359"></a>  assertTrue(&#39;containsValue, should be true, should contain &quot;&quot;&#39;,
<a name="line360"></a>      trie.containsValue(&#39;&#39;));
<a name="line361"></a>  assertTrue(&#39;containsValue, should be true, should contain 0&#39;,
<a name="line362"></a>      trie.containsValue(0));
<a name="line363"></a>  assertTrue(&#39;containsValue, should be true, should contain null&#39;,
<a name="line364"></a>      trie.containsValue(null));
<a name="line365"></a>  assertTrue(&#39;containsValue, should be true, should &#39; +
<a name="line366"></a>      &#39;contain &quot;an empty string key&quot;&#39;,
<a name="line367"></a>      trie.containsValue(&#39;an empty string key&#39;));
<a name="line368"></a>  assertFalse(&#39;containsValue, should be false, should not contain &quot;blah&quot;&#39;,
<a name="line369"></a>      trie.containsValue(&#39;blah&#39;));
<a name="line370"></a>  trie.remove(&#39;empty value&#39;);
<a name="line371"></a>  trie.remove(&#39;zero&#39;);
<a name="line372"></a>  assertFalse(&#39;containsValue, should be false, should not contain 0&#39;,
<a name="line373"></a>      trie.containsValue(0));
<a name="line374"></a>  assertFalse(&#39;containsValue, should be false, should not contain &quot;&quot;&#39;,
<a name="line375"></a>      trie.containsValue(&#39;&#39;));
<a name="line376"></a>}
<a name="line377"></a>
<a name="line378"></a>function testTrieHandlingOfEmptyStrings() {
<a name="line379"></a>  var trie = new goog.structs.Trie();
<a name="line380"></a>  assertEquals(&#39;get, should be undefined&#39;, trie.get(&#39;&#39;), undefined);
<a name="line381"></a>  assertFalse(&#39;containsValue, should be false&#39;, trie.containsValue(&#39;&#39;));
<a name="line382"></a>  assertFalse(&#39;containsKey, should be false&#39;, trie.containsKey(&#39;&#39;));
<a name="line383"></a>  trie.add(&#39;&#39;, &#39;test&#39;);
<a name="line384"></a>  trie.add(&#39;test2&#39;, &#39;&#39;);
<a name="line385"></a>  assertTrue(&#39;containsValue, should be true&#39;, trie.containsValue(&#39;&#39;));
<a name="line386"></a>  assertTrue(&#39;containsKey, should be true&#39;, trie.containsKey(&#39;&#39;));
<a name="line387"></a>  assertEquals(&#39;get, should be &quot;test&quot;&#39;, trie.get(&#39;&#39;), &#39;test&#39;);
<a name="line388"></a>  assertEquals(&#39;get, should be &quot;&quot;&#39;, trie.get(&#39;test2&#39;), &#39;&#39;);
<a name="line389"></a>  trie.remove(&#39;&#39;);
<a name="line390"></a>  trie.remove(&#39;test2&#39;);
<a name="line391"></a>  assertEquals(&#39;get, should be undefined&#39;, trie.get(&#39;&#39;), undefined);
<a name="line392"></a>  assertFalse(&#39;containsValue, should be false&#39;, trie.containsValue(&#39;&#39;));
<a name="line393"></a>  assertFalse(&#39;containsKey, should be false&#39;, trie.containsKey(&#39;&#39;));
<a name="line394"></a>}
<a name="line395"></a>
<a name="line396"></a>function testPrefixOptionOnGetKeys() {
<a name="line397"></a>  var trie = new goog.structs.Trie();
<a name="line398"></a>  trie.add(&#39;abcdefg&#39;, &#39;one&#39;);
<a name="line399"></a>  trie.add(&#39;abcdefghijk&#39;, &#39;two&#39;);
<a name="line400"></a>  trie.add(&#39;abcde&#39;, &#39;three&#39;);
<a name="line401"></a>  trie.add(&#39;abcq&#39;, null);
<a name="line402"></a>  trie.add(&#39;abc&#39;, &#39;four&#39;);
<a name="line403"></a>  trie.add(&#39;xyz&#39;, &#39;five&#39;);
<a name="line404"></a>  assertEquals(&#39;getKeys, should be 1&#39;, trie.getKeys(&#39;xy&#39;).length, 1);
<a name="line405"></a>  assertEquals(&#39;getKeys, should be 1&#39;, trie.getKeys(&#39;xyz&#39;).length, 1);
<a name="line406"></a>  assertEquals(&#39;getKeys, should be 1&#39;, trie.getKeys(&#39;x&#39;).length, 1);
<a name="line407"></a>  assertEquals(&#39;getKeys, should be 4&#39;, trie.getKeys(&#39;abc&#39;).length, 5);
<a name="line408"></a>  assertEquals(&#39;getKeys, should be 2&#39;, trie.getKeys(&#39;abcdef&#39;).length, 2);
<a name="line409"></a>  assertEquals(&#39;getKeys, should be 0&#39;, trie.getKeys(&#39;abcdefgi&#39;).length, 0);
<a name="line410"></a>}
<a name="line411"></a>
<a name="line412"></a>function testGetKeyAndPrefixes() {
<a name="line413"></a>  var trie = makeTrie();
<a name="line414"></a>  // Note: trie has one of its keys as &#39;&#39;
<a name="line415"></a>  assertEquals(&#39;getKeyAndPrefixes, should be 2&#39;,
<a name="line416"></a>               2,
<a name="line417"></a>               goog.object.getCount(trie.getKeyAndPrefixes(&#39;world&#39;)));
<a name="line418"></a>  assertEquals(&#39;getKeyAndPrefixes, should be 2&#39;,
<a name="line419"></a>               2,
<a name="line420"></a>               goog.object.getCount(trie.getKeyAndPrefixes(&#39;hello&#39;)));
<a name="line421"></a>  assertEquals(&#39;getKeyAndPrefixes, should be 2&#39;,
<a name="line422"></a>               2,
<a name="line423"></a>               goog.object.getCount(trie.getKeyAndPrefixes(&#39;hello,&#39;)));
<a name="line424"></a>  assertEquals(&#39;getKeyAndPrefixes, should be 3&#39;,
<a name="line425"></a>               3,
<a name="line426"></a>               goog.object.getCount(trie.getKeyAndPrefixes(&#39;hello, world&#39;)));
<a name="line427"></a>  assertEquals(&#39;getKeyAndPrefixes, should be 1&#39;,
<a name="line428"></a>               1,
<a name="line429"></a>               goog.object.getCount(trie.getKeyAndPrefixes(&#39;hell&#39;)));
<a name="line430"></a>}
<a name="line431"></a>
<a name="line432"></a>function testGetKeyAndPrefixesStartIndex() {
<a name="line433"></a>  var trie = new goog.structs.Trie();
<a name="line434"></a>  trie.add(&#39;abcdefg&#39;, &#39;one&#39;);
<a name="line435"></a>  trie.add(&#39;bcdefg&#39;, &#39;two&#39;);
<a name="line436"></a>  trie.add(&#39;abcdefghijk&#39;, &#39;three&#39;);
<a name="line437"></a>  trie.add(&#39;abcde&#39;, &#39;four&#39;);
<a name="line438"></a>  trie.add(&#39;abcq&#39;, null);
<a name="line439"></a>  trie.add(&#39;q&#39;, null);
<a name="line440"></a>  trie.add(&#39;abc&#39;, &#39;five&#39;);
<a name="line441"></a>  trie.add(&#39;xyz&#39;, &#39;six&#39;);
<a name="line442"></a>  assertEquals(&#39;getKeyAndPrefixes, should be 3&#39;,
<a name="line443"></a>               3,
<a name="line444"></a>               goog.object.getCount(trie.getKeyAndPrefixes(&#39;abcdefg&#39;, 0)));
<a name="line445"></a>  assertEquals(&#39;getKeyAndPrefixes, should be 1&#39;,
<a name="line446"></a>               1,
<a name="line447"></a>               goog.object.getCount(trie.getKeyAndPrefixes(&#39;abcdefg&#39;, 1)));
<a name="line448"></a>  assertEquals(&#39;getKeyAndPrefixes, should be 1&#39;,
<a name="line449"></a>               1,
<a name="line450"></a>               goog.object.getCount(trie.getKeyAndPrefixes(&#39;abcq&#39;, 3)));
<a name="line451"></a>  assertEquals(&#39;getKeyAndPrefixes, should be 0&#39;,
<a name="line452"></a>               0,
<a name="line453"></a>               goog.object.getCount(trie.getKeyAndPrefixes(&#39;abcd&#39;, 3)));
<a name="line454"></a>}
</pre>


</body>
</html>
